package com.wzt.lianfirecontrol.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.wzt.lianfirecontrol.bean.ConstData;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class GetPhotoUtils {

    private int currentPhotoWhat = 0;
    /**
     * 跳转到本地相册
     * @param activity
     * @param what
     */
    public static void skipToLocalImages(Activity activity, int what) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        activity.startActivityForResult(intent, what);
    }

    /**
     * 跳转到拍照
     * @param activity
     * @param what
     */
    public static void skipToPhoto(Activity activity, int what, Uri photoUri) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        activity.startActivityForResult(intent, what);
    }

    /**
     * 自定义图片名，获取照片的file
     */
    public static File createImgFile(Activity activity) {
        //确定文件名
        String fileName = "img_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
        File dir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            dir = Environment.getExternalStorageDirectory();
        } else {
            dir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        }
        File tempFile = new File(dir, fileName);
        try {
            if (tempFile.exists()) {
                tempFile.delete();
            }
            if (!tempFile.createNewFile()) {
                ToastUtils.showToast(activity, "本机不支持该功能，请从相册上传图片");
            }
        } catch (IOException e) {
            return null;
        }

        return tempFile;
    }


    public static void dealWithAddImageAction(Activity activity, Uri uri, AddImageAction addImageAction, int photoWhat, boolean isOpenCamera) {
        if (null != uri) {
            try {
                String path = FileUtil.getAbsolutePath(activity, uri);
                if (StringUtils.isEmpty(path)) {
                    return;
                }
                File file;
                int totalCount = 0;
                do {
                    file = new File(path);
                    if (0 == file.length()) {
                        Thread.sleep(1000);
                        totalCount++;
                    }
                } while (0 == file.length() && totalCount < 8);

                if (null != file && 0 == file.length()) {
                    ToastUtils.showToast(activity, "亲，你选择的图片太大了，我承受不起。");
                    return;
                }

                if ((file.length() / (1024 * 1024)) > 10) {
                    ToastUtils.showToast(activity, "亲，你选择的图片太大了，我承受不起。");
                    return;
                }
                // 如果是拍照上传图片记录路径删除
                if (isOpenCamera) {
                    String photoPath = PreferencesUtils.getString(activity, ConstData.PHOTOPATH, "");
                    if (StringUtils.isEmpty(photoPath)) {
                        PreferencesUtils.putString(activity, ConstData.PHOTOPATH, path);
                    } else {
                        PreferencesUtils.putString(activity, ConstData.PHOTOPATH, photoPath + ";" + path);
                    }
                }
                dealWithAddedImage(activity, path, addImageAction, photoWhat);
            } catch (Exception e) {

            }
        }
    }

    /**
     * 启动压缩
     *
     * @param path
     */
    public static void dealWithAddedImage(final Activity activity, final String path,
                                          final AddImageAction addImageAction, final int photoWhat) {
        try {
            Luban.with(activity)
                    .load(new File(path)) //传入要压缩的图片
                    .setCompressListener(new OnCompressListener() { //设置回调

                        @Override
                        public void onStart() {
                            // TODO 压缩开始前调用，可以在方法内启动 loading UI
                        }

                        @Override
                        public void onSuccess(final File file) {
                            // TODO 压缩成功后调用，返回压缩后的图片文件
                            addImageAction.imgSuccessAction(file, AndroidFileUtil.file2Base64String(file), photoWhat);
                        }

                        @Override
                        public void onError(Throwable e) {
                            // TODO 当压缩过去出现问题时调用
                            ToastUtils.showToast(activity, e.getMessage());
                        }
                    }).launch();
        } catch (Exception e) {
        }
    }

    public interface AddImageAction{
        void imgSuccessAction(File file, String imageStr, int photoWhat);
    }
}
